package com.brillilab.starter.controller.web.instrument;

import com.baomidou.mybatisplus.core.toolkit.support.Property;
import com.brillilab.common.entity.PageVo;
import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.po.instrument.InstrumentAppointment;
import com.brillilab.domain.vo.instrument.AppointmentVo;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.logic.instrument.AppointmentLogic;
import com.brillilab.service.logic.instrument.InstrumentLogic;
import com.brillilab.service.logic.instrument.converter.AppointmentConverter;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.aspect.AspectLog;
import com.brillilab.starter.constant.StarterConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 仪器预约 前端控制器
 * </p>
 *
 * @author WuMenghao
 * @since 2019-06-12
 */
@Slf4j
@RestController
@RequestMapping("/web/instrument/appointment")
public class AppointmentController {

    @Resource
    private AppointmentLogic appointmentLogic;
    @Resource
    private UsersLogic usersLogic;

    /**
     * 新增预约/修改
     *
     * @param appointmentVo
     * @return
     */
    @AspectLog
    @RequestMapping(value="/createOrUpdate", method=RequestMethod.POST)
    public ResponseVo makeAppointment(@RequestBody AppointmentVo appointmentVo,@RequestHeader(value=StarterConstant.TOKEN) String token) {

        Map<String, Object> map=new HashMap<>();

        Assert.isTrue(StringUtils.isNotBlank(token) && appointmentVo.getInstrumId() != null
                && StringUtils.isNotBlank(appointmentVo.getStartDate()) && StringUtils.isNotBlank(appointmentVo.getEndDate()),"请求参数缺失！");


        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        //新增或修改
        InstrumentAppointment appointment=appointmentLogic.createOrUpdateNewAppointment(appointmentVo,userInfo);


        return ResponseVo.success(appointment);

    }

    /**
     * 取消预约
     * @return
     */
    @AspectLog
    @RequestMapping(value="/cancel",method=RequestMethod.DELETE)
    public ResponseVo cancelAppointment(Long id,@RequestHeader(value=StarterConstant.TOKEN) String token){

        Assert.isTrue(id!=null && StringUtils.isNotBlank(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        appointmentLogic.cancelAppointment(id,userInfo);

        return ResponseVo.success();
    }

    /**
     * 预约列表
     * @param instrumId
     * @return
     */
    @RequestMapping(value="/list",method=RequestMethod.GET)
    public ResponseVo AppointmentList(@RequestParam(name="instrumId") Long instrumId,
                                      @RequestParam(name="pageNum",defaultValue="1") Integer pageNum,
                                      @RequestParam(name="pageSize",defaultValue="10") Integer pageSize,
                                      @RequestParam(name="appointType") Integer appointType,
                                      @RequestHeader(value=StarterConstant.TOKEN) String token){

        //校验参数
        Assert.isTrue(appointType!=null && StringUtils.isNotBlank(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        //获取预约列表
        PageVo<AppointmentVo> appointmentPageVo=appointmentLogic.getAppointmentList(instrumId,userInfo,appointType,pageNum,pageSize);


        return ResponseVo.success(appointmentPageVo);
    }

    /**
     * 在日程插件中显示预约
     * @return
     */
    @RequestMapping(value="/schedulers",method=RequestMethod.GET)
    public ResponseVo getAllAppointmentOnScheduler(Long instrumId,@RequestHeader(value=StarterConstant.TOKEN) String token){

        Assert.isTrue(instrumId!=null && StringUtils.isNotBlank(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        List<AppointmentVo> appointmentVos=appointmentLogic.getAllAppointmentOnScheduler(instrumId,userInfo);

        return ResponseVo.success(appointmentVos);
    }
}
